import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;

import javax.swing.*;

import planete.*;

public class Planete extends JPanel implements KeyListener,
		MouseMotionListener, MouseListener, MouseWheelListener, Runnable {
	private ArrayList<Planeta> planete = new ArrayList<Planeta>();
	private boolean init = false;
	private Paint pozadina = new Color(10, 10, 10);
	private Color[] boje = { Color.yellow, Color.white, Color.gray, Color.red,
			Color.orange, Color.green, Color.blue };
	private Rectangle okvir;
	private Dimension velicinaProzora;

	private String nazivNovePlanete = "";

	private int planetaUCentru;

	public Planete() {
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	public void init() {
		if (!init) {
			Dimension d = getSize();
			double center_x = d.width / 2, center_y = d.height / 2;

			planete.add(new Planeta("Neptun", 25 + Math.random() * 150,
					new Vektor(), new Vektor(center_x + Math.random() * 50,
							center_y + Math.random() * 50, 0), boje[(int) (Math
							.random() * boje.length)], boje[(int) (Math
							.random() * boje.length)]));
			planete.add(new Planeta("Uran", 25 + Math.random() * 150,
					new Vektor(), new Vektor(center_x + Math.random() * 50,
							center_y + Math.random() * 50, 0), boje[(int) (Math
							.random() * boje.length)], boje[(int) (Math
							.random() * boje.length)]));
			planete.add(new Planeta("Pluton", 25 + Math.random() * 150,
					new Vektor(), new Vektor(center_x + Math.random() * 50,
							center_y + Math.random() * 50, 0), boje[(int) (Math
							.random() * boje.length)], boje[(int) (Math
							.random() * boje.length)]));
			planete.add(new Planeta("Venera", 25 + Math.random() * 150,
					new Vektor(), new Vektor(center_x + Math.random() * 50,
							center_y + Math.random() * 50, 0), boje[(int) (Math
							.random() * boje.length)], boje[(int) (Math
							.random() * boje.length)]));
			planete.add(new Planeta("Zemlja", 25 + Math.random() * 150,
					new Vektor(), new Vektor(center_x + Math.random() * 50,
							center_y + Math.random() * 50, 0), boje[(int) (Math
							.random() * boje.length)], boje[(int) (Math
							.random() * boje.length)]));

			velicinaProzora = d;

			planetaUCentru = (int) (Math.random() * planete.size());

			init = true;
		}
	}

	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		Dimension d = getSize();
		if (okvir == null || !d.equals(velicinaProzora)) {
			okvir = new Rectangle((int) d.getWidth(), (int) d.getHeight());
			velicinaProzora = d;
		}

		g2d.setPaint(pozadina);
		g2d.fill(okvir);

		for (int i = 0; i < planete.size(); i++) {
			if (planete.get(i) != null)
				planete.get(i).draw(g2d);
		}
	}

	// KeyListener implementation
	public void keyPressed(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
		switch (e.getKeyChar()) {

		}

		if (e.getKeyChar() == KeyEvent.VK_ENTER) {
			Dimension d = getSize();
			double center_x = d.width / 2, center_y = d.height / 2;
			planete.add(new Planeta(nazivNovePlanete, 25 + Math.random() * 150,
					new Vektor(), new Vektor(center_x + Math.random() * 50,
							center_y + Math.random() * 50, 0), boje[(int) (Math
							.random() * boje.length)], boje[(int) (Math
							.random() * boje.length)]));

			nazivNovePlanete = "";
		} else {
			nazivNovePlanete = nazivNovePlanete + e.getKeyChar();
		}

		// repaint();
	}

	// MouseListener implementation
	public void mouseClicked(MouseEvent e) {
		

	}

	public boolean tackaPripadaPlaneti(int x,int y,Planeta p){
		Vektor pozicija=p.pozicija();
		double masa=p.masa();
		double scale=1+masa/10;
		Ellipse2D.Double f=new Ellipse2D.Double((pozicija.x()-1)*scale,(pozicija.y()-1)*scale,2*scale,2*scale);
		if(f.contains(x, y))return true;
		else return false;
	}
	public void mouseReleased(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		// repaint();
				// da se ustanovi koja je planeta kliknuta a samim tim da se vidi koja
				// ce postati u centru planeta
				int x = e.getX();
				int y = e.getY();
				for (int i = 0; i < planete.size(); i++) {
					Planeta p = planete.get(i);
					if (tackaPripadaPlaneti(x, y, p)) {
						planetaUCentru = i;
						break;
					}

				}
	}

	public void mouseEntered(MouseEvent e) {
	}

	// MouseMotionListener implementation
	public void mouseMoved(MouseEvent e) {
	}

	public void mouseDragged(MouseEvent e) {
	}

	// MouseWheelListener implementation
	public void mouseWheelMoved(MouseWheelEvent e) {
	}

	private void pomeriPlanete() {
		for (int pl_index = 0; pl_index < planete.size(); pl_index++) {
			Vektor ubrzanje = new Vektor();

			for (int i = 0; i < planete.size(); i++) {
				if (i != pl_index) // racuna se sila ostalih planeta na ovu
				{
					double razdaljina2 = planete.get(pl_index).razdaljina2(
							planete.get(i));

					ubrzanje.dodaj(planete.get(pl_index)
							.pravacKa(planete.get(i))
							.proizvod(planete.get(i).masa() / razdaljina2));
				}
			}

			planete.get(pl_index).gUbrzanje(ubrzanje);
		}

		for (int j = 0; j < planete.size(); j++)
			planete.get(j).protekloVreme(0.1);

		// da se pomera pregled glavnog prozora
		Dimension d = getSize();
		Vektor tekucaPoz = planete.get(planetaUCentru).pozicija();
		if (tekucaPoz.x() < 0.05 * d.getWidth()) {// svi idu u desno
			for (Planeta p : planete) {
				p.pozicija().setX(p.pozicija().x() + 0.45 * d.getWidth());
			}
		}
		if (tekucaPoz.x() > 0.95 * d.getWidth()) {// svi idu u levo
			for (Planeta p : planete) {
				p.pozicija().setX(p.pozicija().x() - 0.45 * d.getWidth());
			}
		}
		if (tekucaPoz.y() < 0.05 * d.getHeight()) {// svi idu na dole
			for (Planeta p : planete) {
				p.pozicija().setY(p.pozicija().y() + 0.45 * d.getHeight());
			}
		}
		if (tekucaPoz.y() > 0.95 * d.getHeight()) {// svi idu na gore
			for (Planeta p : planete) {
				p.pozicija().setY(p.pozicija().y() - 0.45 * d.getHeight());
			}
		}

	}

	public void run() {
		long starttime;

		init();

		while (true) {
			starttime = System.currentTimeMillis();
			try {
				repaint();
				pomeriPlanete();
				starttime += 40;
				Thread.sleep(Math.max(0, starttime - System.currentTimeMillis()));
			} catch (InterruptedException e) {
				break;
			}
		}
	}

	public static void main(String s[]) {
		Planete panel = new Planete();

		panel.setOpaque(true);

		JFrame frame = new JFrame("Planete - skelet");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		frame.setLayout(new BorderLayout());
		frame.getContentPane().add("Center", panel);
		// JPanel p = new JPanel();
		// p.setPreferredSize(new Dimension(800, 100));
		// frame.getContentPane().add("South", p);
		frame.setSize(new Dimension(800, 600));
		frame.setVisible(true);
		frame.addKeyListener(panel);
		frame.addMouseWheelListener(panel);

		Thread t = new Thread(panel);
		t.start();
	}
}